<template>
  <div class="app-container">
    <div class="project-list-view">
      <el-table :data="tableData3"
                border
                style="width: 100%"
                :row-key="tableData3.pro_id">
        <el-table-column prop="pro_id"
                         label="ID"
                         width="50">
        </el-table-column>
        <el-table-column prop="pro_title"
                         label="项目标题">
        </el-table-column>
        <el-table-column prop="pro_biography"
                         label="项目简介">
        </el-table-column>
        <el-table-column prop="users_model.nickname"
                         label="作者"
                         width="100">
        </el-table-column>
        <el-table-column prop="tags"
                         label="项目标签"
                         width="150">
          <template slot-scope="scope">
            <el-tag size="mini"
                    v-for="tag of JSON.parse(scope.row.tags)">{{ tag }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pro_path"
                         label="项目内容"
                         width="80">
          <template slot-scope="scope">
            <el-button @click="handleDownloadProject(scope.row.pro_path)"
                       type="primary"
                       plain
                       size="small">下载</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="update_time"
                         label="创建时间"
                         width="200">
        </el-table-column>
        <el-table-column prop="setting"
                         label="详细设置"
                         width="80">
          <template slot-scope="scope">
            <el-popover ref="popover4"
                        placement="left"
                        width="200"
                        trigger="click">
              <el-switch style="display: block"
                         v-model="projectSelected"
                         active-color="#13ce66"
                         inactive-color="#ff4949"
                         active-text="设为精选"
                         inactive-text="取消精选"
                         active-value="1"
                         inactive-value="0"
                         @change="handleChangeSelected(scope.row.pro_id)">
              </el-switch>
              <div style="height: 20px"></div>
              <el-switch style="display: block"
                         v-model="projectHot"
                         active-color="#13ce66"
                         inactive-color="#ff4949"
                         active-text="设为热门"
                         inactive-text="取消热门"
                         active-value="1"
                         inactive-value="0"
                         @change="handleChangeHot(scope.row.pro_id)">
              </el-switch>
            </el-popover>
            <el-button @click="handleSetting(scope.row)"
                       type="primary"
                       plain
                       size="small"
                       v-popover:popover4>设置</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="delete"
                         label="删除"
                         width="80">
          <template slot-scope="scope">
            <el-button @click="handleDelete(scope.row.pro_id)"
                       type="danger"
                       plain
                       size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {
  getProjectList,
  updateProjectSelected,
  updateProjectHot,
  delProject,
} from '@/api/projectList'
export default {
  name: 'ProjectTag',
  computed: {},
  data() {
    return {
      tableData3: [],
      projectSelected: '0',
      projectHot: '0',
    }
  },
  methods: {
    // 获取项目信息
    getProjectInfo: function () {
      return getProjectList().then((res) => {
        this.tableData3.splice(0, this.tableData3.length)
        this.tableData3.push(...res.data.rows)
      })
    },
    // 根据项目文件地址下载项目文件
    handleDownloadProject: function (url) {
      console.log('download', JSON.parse(url))
    },
    // 项目设置按钮，初始化内部所需参数
    handleSetting: function (info) {
      let selected = info.selected.toString()
      let hot = info.hot.toString()
      this.projectSelected = selected
      this.projectHot = hot
    },
    // 删除项目
    handleDelete: function (id) {
      console.log('删除')
      delProject({id:id}).then((res) => {
        if (res.data.status === 200) {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'success',
          }),
            this.getProjectInfo()
        }
      })
    },
    // 项目精选开关
    handleChangeSelected: function (projectId) {
      updateProjectSelected({
        pro_id: projectId,
        selected: this.projectSelected,
      }).then((res) => {
        if (res.data.status == 200) {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'success',
          }),
            this.getProjectInfo()
        } else {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'warning',
          })
        }
      })
    },
    // 项目热门开关
    handleChangeHot: function (projectId) {
      updateProjectHot({
        pro_id: projectId,
        hot: this.projectHot,
      }).then((res) => {
        if (res.data.status == 200) {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'success',
          }),
            this.getProjectInfo()
        } else {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'warning',
          })
        }
      })
    },
  },
  beforeMount() {
    this.getProjectInfo()
  },
}
</script>

<style lang="scss"></style>